package com.bdmc.task.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.task.domain.TaskPlanDetail;
import com.bdmc.task.domain.bo.TaskPlanDetailBo;
import com.bdmc.task.domain.vo.PersonWorkTimeVo;
import com.bdmc.task.domain.vo.TaskPlanDetailVo;
import com.bdmc.task.mapper.TaskPlanDetailMapper;
import com.bdmc.task.service.ITaskPlanDetailService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 每日排班详情Service业务层处理
 *
 * @author bdmc
 * @date 2022-03-06
 */
@Service
public class TaskPlanDetailServiceImpl extends ServicePlusImpl<TaskPlanDetailMapper, TaskPlanDetail, TaskPlanDetailVo> implements ITaskPlanDetailService {

    @Override
    public TaskPlanDetailVo queryById(Integer planDetailId){
        return getVoById(planDetailId);
    }

    @Override
    public TableDataInfo<TaskPlanDetailVo> queryPageList(TaskPlanDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TaskPlanDetail> lqw = buildQueryWrapper(bo);
        Page<TaskPlanDetailVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<TaskPlanDetailVo> mySelectPage(TaskPlanDetailBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.mySelectPage(pageQuery.build(),bo));
    }

    @Override
    public List<TaskPlanDetailVo> queryList(TaskPlanDetailBo bo) {
        LambdaQueryWrapper<TaskPlanDetail> lqw = buildQueryWrapper(bo);
        return listVo(lqw);
    }

    @Override
    public List<TaskPlanDetailVo> selectTaskPlanDetailByWorkPlanId(Long workPlanId) {
        return baseMapper.selectTaskPlanDetailByWorkPlanId(workPlanId);
    }

    @Override
    public Map<String, Object> selectWorkPersonOnlineCount(Long deptId) {
        return baseMapper.selectWorkPersonOnlineCount(deptId);
    }

    @Override
    public List<PersonWorkTimeVo> selectPersonDailyWorkTime(String effectDate) {
        return baseMapper.selectPersonDailyWorkTime(effectDate);
    }


    private LambdaQueryWrapper<TaskPlanDetail> buildQueryWrapper(TaskPlanDetailBo bo) {
        LambdaQueryWrapper<TaskPlanDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getWorkPlanId() != null, TaskPlanDetail::getWorkPlanId, bo.getWorkPlanId());
        lqw.eq(bo.getEffectDate() != null, TaskPlanDetail::getEffectDate, bo.getEffectDate());
        lqw.eq(bo.getWorkTeamId() != null, TaskPlanDetail::getWorkTeamId, bo.getWorkTeamId());
        lqw.eq(bo.getDetailStatus() != null, TaskPlanDetail::getDetailStatus, bo.getDetailStatus());
        return lqw;
    }

    @Override
    public Boolean insertByBo(TaskPlanDetailBo bo) {
        TaskPlanDetail add = BeanUtil.toBean(bo, TaskPlanDetail.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setPlanDetailId(add.getPlanDetailId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(TaskPlanDetailBo bo) {
        TaskPlanDetail update = BeanUtil.toBean(bo, TaskPlanDetail.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TaskPlanDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
